/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
* Name: directx_audio_sink.h
*
* Purpose: audio sink component implementation with directx audio.
*
* Developer:
*   wen.gu , 2018-09-05
*
* TODO:
*
***************************************************************************/
#ifndef __DIRECTX_AUDIO_SINK_H__
#define __DIRECTX_AUDIO_SINK_H__

/******************************************************************************
**    INCLUDES
******************************************************************************/
#include <string.h>
#include <windows.h>
#include <mmsystem.h>
#include <process.h>
#include <list>

#include "mp_audio_sink.h"

/******************************************************************************
**    MACROS
******************************************************************************/


/******************************************************************************
**    TYPE DEFINITIONS
******************************************************************************/
struct WaveHdrNode
{
    WAVEHDR hdr;
    WaveHdrNode* next;
};

/******************************************************************************
**    CLASSES/FUNCTIONS DEFINITIONS
******************************************************************************/

class DxAudioSink: public MPAudioSink
{
public:
    DxAudioSink();
    ~DxAudioSink();

public:
    virtual const MPTrackInfo& getFormat() override;
    virtual MPError start() override;
    virtual MPError pause() override;
    virtual MPError flush(eAudioSinkFlush do_what = eAudioSinkFlush::Drain) override;

    /** Queues a buffer of samples for playback,
    *  external needn't do buffer release.
    */
    virtual MPError write(MPBuffer* data) override;

public:
    /** self API */
    MPError initialize(const MPTrackInfo& info);

    void onWaveDone(WaveHdrNode* p_waveheader);

protected:
    MPError WaveOutOpen(const MPTrackInfo& info);
    void WaveOutClose();
    MPError WaveOutPause();
    MPError WaveOutResume();
    MPError WaveOutFlush(eAudioSinkFlush do_what);
    MPError PlayWaveOut(WaveHdrNode *p_waveheader, MPBuffer *p_buffer);
    MPError WaveOutWrite(MPBuffer* buffer);
    

    void WaveOutClean();
    void WaveOutClearBuffer(WAVEHDR *p_waveheader);
private:
    HWAVEOUT mWaveOut = nullptr;
    bool mIsRequestExit = false; /** GTRUE: request exit, GFALSE: not */
    WaveHdrNode* mFreeList = nullptr;
    uint32_t mFramesCnt; /** how many frames wrote to waveout */

    MPTrackInfo mInfo;
    std::mutex mLock;
    std::condition_variable mCond;
};

#endif /** !__DIRECTX_AUDIO_SINK_H__*/